<?php

declare(strict_types=1);

namespace app\admin\validate;

use think\Validate;
use think\facade\Db;

class Customer extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        // 'ids'         => 'require|checkIds',
        'id'          => 'integer|gt:0',
        'cusname'     => 'require|max:50',
        'phone'       => 'checkUnique:phone',
        'email'       => 'email|checkUnique:email',
        'qq'          => 'max:50',
        'wechat'      => 'max:50',
        'whatsapp'    => 'max:50',
        'dingtalk'    => 'max:50',
        'country'     => 'max:9',
        'address'     => 'max:255',
        'status'      => 'integer|in:1,2,3',
        'stage'       => 'integer|in:1,2,3',
        'tags'        => 'max:255',
        'loss'        => 'max:255',
        'curator'     => 'max:255',
        'custype'     => 'require|max:9',
        'cusnature'   => 'require|max:9',
        'source'      => 'max:9',
        'att_path'    => 'max:2000',
        'remark'      => 'max:255',
        'creator'     => 'integer',
        'create_time' => 'dateFormat:Y-m-d H:i:s',
        'update_time' => 'dateFormat:Y-m-d H:i:s',
        'role'        => 'require|in:user,distributor',
    ];

    protected $scene = [
        'show' => ['id'],
        'create' => ['cusname', 'phone', 'email', 'custype', 'cusnature'],
        'search' => [
            'pageIndex' => 'require|checkPageIndex',
            'pageSize' => 'require|checkPageSize', // 假设最大每页100条记录
            'cusname' => 'max:50',
            'status',
            'stage',
            'cusnature' => 'max:9',
        ],
        'update' => ['cusname' => 'max:50', 'phone', 'email',  'cusnature' => 'max:9', 'cusnature' => 'max:9'],
        'delete' => ['ids'],
        'allocation' => ['id' => 'require|integer|gt:0', 'allocationId' => 'require|integer|gt:0'],
        'operation' => ['id' => 'require|integer|gt:0'],
        'approvalRecords' => ['id' => 'require|integer|gt:0'],
        'role' => ['id' => 'require|integer|gt:0', 'role' => 'require|in:user,distributor'],
    ];


    protected function checkIds($value, $rule, $data = [])
    {
        // 如果是单个整数字符串，直接验证
        if (is_numeric($value) && intval($value) == $value && intval($value) > 0) {
            return true;
        }

        // 如果是字符串，先转换为数组
        if (is_string($value)) {
            $valueArray = explode(',', $value);
        } else {
            return 'IDs必须是逗号分隔的字符串或单个整数字符串';
        }

        // 验证数组中的每个ID
        foreach ($valueArray as $id) {
            if (!is_numeric($id) || intval($id) != $id || intval($id) <= 0) {
                return '每个ID必须是正整数';
            }
        }
        return true;
    }



    protected function checkPageIndex($value, $rule, $data = [])
    {

        if ($value === '') {
            return false;
        }
        return $value >= 1;
    }

    protected function checkPageSize($value, $rule, $data = [])
    {
        if ($value === '') {
            return false;
        }
        return $value >= 1 && $value <= 100;
    }

    protected function checkUnique($value, $field, $data)
    {
        $where = [
            $field   => $value,
            'is_del' => 0,
        ];

        if (isset($data['id'])) {
            $where[] = ['id', '<>', $data['id']];
        }

        return Db::name('customer')->where($where)->find() ? false : true;
    }

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'id.integer'          => 'ID 必须是整数',
        'id.gt'              => 'ID 必须大于0',
        'cusname.require'     => '客户名称必须',
        'cusname.max'         => '客户名称最多不能超过50个字符',
        'phone.mobile' => '手机号码格式不正确',
        'phone.checkUnique' => '手机号码已存在',
        'email.email' => '电子邮件格式不正确',
        'email.checkUnique' => '电子邮件地址已存在',
        'qq.max'              => 'QQ最多不能超过50个字符',
        'wechat.max'          => '微信最多不能超过50个字符',
        'whatsapp.max'        => 'WhatsApp最多不能超过50个字符',
        'dingtalk.max'        => '钉钉最多不能超过50个字符',
        'country.max'         => '国家最多不能超过9个字符',
        'address.max'         => '地址最多不能超过255个字符',
        'status.integer'      => '客户状态必须是整数',
        'status.in'           => '客户状态必须是2(忠诚客户)、1(成交客户)或0(无效客户)',
        'stage.integer'       => '客户阶段必须是整数',
        'stage.in'            => '客户阶段必须是2(成交)、1(确认需求)或0(流失)',
        'tags.max'            => '标签最多不能超过255个字符',
        'loss.max'            => '流失原因最多不能超过255个字符',
        'curator.max'         => '负责人最多不能超过255个字符',
        'custype.max'         => '客户类型最多不能超过9个字符',
        'cusnature.max'       => '客户性质最多不能超过9个字符',
        'source.max'          => '来源最多不能超过9个字符',
        'att_path.max'        => '附件路径最多不能超过2000个字符',
        'remark.max'          => '备注最多不能超过255个字符',
        'creator.integer'     => '创建人必须是整数',
        'role.require'        => '角色不能为空',
        'role.in'             => '角色必须是user或distributor',
    ];
}
